<?php
// 首页显示信息
namespace NewAdmin\Controller;
use Think\Controller;
class SystemController extends CommonController {
    /**
     * 后台栏目管理
    */
    function menu(){
        $menu   =   D('Menu');
        $this->assign('title','后台栏目管理');
          
        if(empty($_REQUEST['cid'])&&$_SESSION['admin_type']==null){
            $type=0;
            $_SESSION['admin_type']=$type;
        }else{
            if($_REQUEST['cid']==null){
                $type=$_SESSION['admin_type'];
            }else{
                $type=$_REQUEST['cid'];
                $_SESSION['admin_type']=$type;
            }
        
        }

        $menudatashowArray  =   $menu->where('bclassid = 0 AND type = '.$type)->order('sort ASC')->select();
        $this->assign('menuArrayFlag',$menudatashowArray);
        $this->assign('type',$type);
        $this->display('Menu:list');
    }
    
    /**
     * 栏目添加
    */
    function menuAdd(){
        $this->assign('title','栏目添加');
        $menu   =   D('Menu');
        $menuCurrent=   $menu->where('id = '.$_GET['id'])->find();
        $menuCurrent['type']    =   $menuCurrent['type']?$menuCurrent['type']:$_SESSION['admin_type'];
        $menuArray  =   $menu->where('bclassid = 0 AND  type = '.$menuCurrent['type'])->select();
        $this->assign('menuArrayFlag',$menuArray);
        $this->assign('menuCurrent',$menuCurrent);
        $this->assign('admin_type',$_SESSION['admin_type']);
        $this->display('Menu:add');
    }
    
    /**
     * 栏目添加保存
    */
    function menuSave(){
        $menu   =   D('Menu');
        $id     =   $_POST['id'];
        $data['id'] =   $_POST['id'];
        $new_data   =   array_diff_key($_POST,$data);
        $new_data['isOnlyAdmin']	=	$new_data['isOnlyAdmin']?$new_data['isOnlyAdmin']:0;
        $new_data['isDefault']		=	$new_data['isDefault']?$new_data['isDefault']:0;
        
        if( $id ){
            $flag   =   $menu->where(' id = '.$id)->save($new_data);
        }else{
            $flag   =   $menu->add($new_data);
        }
        
        echo $flag !== false?'0|||操作成功|||/system/menu/type/'.$_POST['type']:'0|||操作失败|||reload';die();
    }
    
    /**
     * 栏目禁用
    */
    function menuDelete(){
        $menu   =   D('Menu');
        $id     =   $_GET['id'];
        $data['ishidden'] =   $_GET['ishidden'];
        
        $flag   =   $menu->where(' id = '.$id)->save($data);
        
        urlSkip($flag,'/system/menu/',1);
    }
	
	/**
	 *	子栏目加载
	*/
	function menuChildren(){
		$menu =   D('Menu');
        $moduleId   =   $_GET['bclassid'];
        $contentFlag    =   '';
        $menuChildrenArray   =   $menu->where('  bclassid = '.$moduleId)->order('sort ASC')->select();
        if( $menuChildrenArray ){
            foreach( $menuChildrenArray AS $key=>$row ){
                $isHidden   =   $row['ishidden']?'显示':'隐藏';
                $ishiddenFlag   =   $row['ishidden']?'0':'1';
                if($menuChildrenArray[$key+1]){
                     $contentFlag    .=  '
                    <tr class="list childList listChild'.$moduleId.'">
                        <td>'.$row['id'].'</td>
                        <td>├─'.$row['name'].'</td>
                        <td>'.$row['module'].'</td>
                        <td>'.$row['action'].'</td>
                      
                        <td>'.$row['sort'].'</td>
                        <td>
                            <a href="'.'/system/menuAdd/id/'.$row['id'].'">修改</a>&nbsp;&nbsp;
                            <a href="'.'/system/menuDelete/id/'.$row['id'].'/ishidden/'.$ishiddenFlag.'">'.$isHidden.'</a>
                        </td>
                    </tr>';
                }else{
                     $contentFlag    .=  '
                    <tr class="list childList listChild'.$moduleId.'">
                        <td>'.$row['id'].'</td>
                        <td>└─'.$row['name'].'</td>
                        <td>'.$row['module'].'</td>
                        <td>'.$row['action'].'</td>
                      
                        <td>'.$row['sort'].'</td>
                        <td>
                            <a href="'.'/system/menuAdd/id/'.$row['id'].'">修改</a>&nbsp;&nbsp;
                            <a href="'.'/system/menuDelete/id/'.$row['id'].'/ishidden/'.$ishiddenFlag.'">'.$isHidden.'</a>
                        </td>
                    </tr>';
                }
               
            }
        }
        echo $contentFlag;
	}
	
	/**
	 *	添加栏目,上级栏目加载
	*/
	function menuClassAjax(){
		$menu =   D('Menu');
        $type   =   $_GET['type'];
        $contentFlag    =   '<option value="">请选择上级栏目</option>';
        $menuArray   =   $menu->where(' bclassid = 0 AND ishidden = 0 AND type = '.$type)->select();
        if( $menuArray ){
            foreach( $menuArray AS $key=>$row ){
                $contentFlag    .=  '<option value="'.$row['id'].'" menuModule="'.$row['module'].'">'.$row['name'].'</option>';
            }
        }
        echo $contentFlag;
	}

      public function area()
    {
        $area = M('sys_area');
        $province = M('sys_province');
        $city = M('sys_city');


        $area_list = $area->field('area_id,area_name')->select(); // 区域地址
        $list = $province->field('province_id,area_id,province_name,sort')->order('sort asc')->select();//省份
        foreach ($list as $k => $v) {
            $rs=$city->where('province_id='.$v['province_id'])->count();
            if ($rs > 0) {
                $list[$k]['issetLowerLevel'] = 1;
            } else {
                $list[$k]['issetLowerLevel'] = 0;
            }
            if (! empty($area_list)) {
                foreach ($area_list as $area) {
                    if ($area['area_id'] == $v['area_id']) {
                        $list[$k]['area_name'] = $area['area_name'];
                        break;
                    }
                }
            }
        }
        
        $this->assign("area_list", $area_list);
        $this->assign("list", $list);
        $this->assign("title", '地区管理');
        $this->display();
    }

     public function selectCityListAjax()
    {

        if ($_POST) {

            $province_id = I('province_id');
           
            $city = M('sys_city');
            $district = M('sys_district');
            $list = $city->where('province_id='.$province_id)->select();

            foreach ($list as $k=>$v) {
                $rs=$district->where('city_id='.$v['city_id'])->count();
                if ($rs > 0) {
                    $list[$k]['issetLowerLevel'] = 1;
                } else {
                    $list[$k]['issetLowerLevel'] = 0;
                }
            }
            $this->ajaxReturn($list);
        }
    }

     public function selectDistrictListAjax()
    {
        if ($_POST) {
            $city_id = I('city_id');
            $district = M('sys_district');
            $list = $district->where('city_id='.$city_id)->select();
            $this->ajaxReturn($list);
        }
    }
    public function addProvinceAjax()
    {
       if ($_POST) {
            $province = M('sys_province');
           
            $province_name = I('post.regionName'); // 区域名称
            $sort = I('post.regionSort'); // 排序
            $area_id = I('post.area_id',0); // 区域id

            $data = array(
                "province_name" => $province_name,
                "sort" => $sort,
                "area_id" => $area_id
            );
            $res = $province->add($data);
            if($res){
                $mseeage['code']=1;
                $mseeage['message']='添加成功';
            }else{
                $mseeage['code']=0;
                $mseeage['message']='添加失败';
            }
            $this->ajaxReturn($mseeage);
        
        }
    }

    public function addCityAjax()
    {
         if ($_POST) {
            $city =  M('sys_city');
            $city_id = 0;
            $province_id =I('post.superiorRegionId');
            $city_name =I('post.regionName');
            $zipcode = I('post.zipcode');
            $sort = I('post.regionSort');
            $header_codeval = I('post.header_codeval');

            $data = array(
                "province_id" => $province_id,
                "city_name" => $city_name,
                "zipcode" => $zipcode,
                "sort" => $sort,
                "header_code" => $header_codeval
            );
            $res = $city->add($data);
             if($res){
                $mseeage['code']=1;
                $mseeage['message']='添加成功';
            }else{
                $mseeage['code']=0;
                $mseeage['message']='添加失败';
            }
            $this->ajaxReturn($mseeage);
        }
    }

    public function addDistrictAjax()
    {
        if ($_POST) {
            $district =  M('sys_district');
            $district_id = 0;
            $city_id = I('post.superiorRegionId');
            $district_name = I('post.regionName');
            $sort = I('post.regionSort');
            $data = array(
                "city_id" => $city_id,
                "district_name" => $district_name,
                "sort" => $sort
            );
             $res = $district->add($data);
             if($res){
                $mseeage['code']=1;
                $mseeage['message']='添加成功';
            }else{
                $mseeage['code']=0;
                $mseeage['message']='添加失败';
            }
            $this->ajaxReturn($mseeage);
        }
    }

    public function updateDistrictAjax()
    {
         if ($_POST) {
            $district =  M('sys_district');
            $district_id = I('post.eventId');
            $city_id =I('post.superiorRegionId');
            $district_name =I('post.regionName');
            $sort = I('post.regionSort');
            $data = array(
                "city_id" => $city_id,
                "district_name" => $district_name,
                "sort" => $sort
            );
            if ($district_id > 0 && $district_id != 0) {
                $data['district_id']=$district_id;
                $res=$district->save($data);
            } else {
                $res=$district->add($data);
            }
            $this->ajaxReturn($res);  
        }
    }

    public function updatecityajax()
    {
        if ($_POST) {
            $city =  M('sys_city');
            $city_id = I('post.eventId');
            $province_id =I('post.superiorRegionId');
            $city_name =I('post.regionName');
            $sort = I('post.regionSort');
            $zipcode = I('post.zipcode');

            $header_codeval = I('post.header_codeval');
            $data = array(
               "province_id" => $province_id,
                "city_name" => $city_name,
                "zipcode" => $zipcode,
                "header_code" => $header_codeval,
                "sort" => $sort
            );
            if ($city_id > 0 && $city_id != 0) {
                $data['city_id']=$city_id;
                $res=$city->save($data);
            } else {
                $res=$city->add($data);
            }
            $this->ajaxReturn($res);
        }
    }

     public function updateRegionAjax()
    {
        if ($_POST) {
            $upType =I('post.upType');
            $regionType = I('post.regionType');
            $regionName = I('post.regionName');
            $regionSort = I('post.regionSort');
            $regionId = I('post.regionId');
            $res = $this->updateRegionNameAndRegionSort($upType, $regionType, $regionName, $regionSort, $regionId);
            $this->ajaxReturn($res);
        }
    } 

    /**
     * $upType 修改类型 1排序 2名称
     * $regionType 修改地区类型 1省 2市 3县
     */
    public function updateRegionNameAndRegionSort($upType, $regionType, $regionName, $regionSort, $regionId)
    {
        if ($regionType == 1) {
            $province = M('sys_province');
            if ($upType == 1) {
                $data['sort']=$regionSort;
                $data['province_id']=$regionId;
                $res = $province->save($data);
                return $res;
            }
            if ($upType == 2) {
                $data['province_name']=$regionName;
                $data['province_id']=$regionId;
                $res = $province->save($data);
                return $res;
            }
        }
        if ($regionType == 2) {
            $city =  M('sys_city');
            if ($upType == 1) {
                $data['sort']=$regionSort;
                $data['city_id']=$regionId;
                $res = $city->save($data);
                return $res;
            }
            if ($upType == 2) {
                $data['city_name']=$regionName;
                $data['city_id']=$regionId;
                $res = $city->save($data);
                return $res;
            }
        }
        if ($regionType == 3) {

            $district =  M('sys_district');
            if ($upType == 1) {
                $data['sort']=$regionSort;
                $data['district_id']=$regionId;
                $res = $district->save($data);
                return $res;
            }
            if ($upType == 2) {
                $data['district_name']=$regionName;
                $data['district_id']=$regionId;
                $res = $district->save($data);
                return $res;
            }
        }
    }  
     
     public function deleteRegion()
    {
       if ($_POST) {
            $type =I('post.type');
            $regionId =I('post.regionId');
          
            if ($type == 1) {
                $res = $this->deleteProvince($regionId);
            }
            if ($type == 2) {
                $res = $this->deleteCity($regionId);
            }
            if ($type == 3) {
                $res = $this->deleteDistrict($regionId);
            }
            if($res){
                $data['code']=1;
                $data['message']='删除成功';
            }else{
                 $data['code']=0;
                $data['message']='删除失败';
            }
            $this->ajaxReturn($data);
        }
    }

     /**
     * (non-PHPdoc)
     *
     * @see \ata\api\IAddress::deleteProvince()
     */
    public function deleteProvince($province_id)
    {
        $province = M('sys_province');
        $city = M('sys_city');
        $province->startTrans();
        try {
            $city_list = $city->where('province_id='.$province_id)->select();
            foreach ($city_list as $k => $v) {
                $this->deleteCity($v['city_id']);
            }
            $province->delete($province_id);
            $province->commit();
            return 1;
        } catch (\Exception $e) {
            $province->rollback();
            return $e->getMessage();
        }
    }

    /**
     * (non-PHPdoc)
     *
     * @see \ata\api\IAddress::deleteCity()
     */
    public function deleteCity($city_id)
    {
        $city = M('sys_city');
        $district = M('sys_district');
        $city->startTrans();
        try {
            $district->where('city_id='.$city_id)->delete();
            $city->delete($city_id);
            $city->commit();
            return 1;
        } catch (\Exception $e) {
            $city->rollback();
            return $e->getMessage();
        }
    }

    /**
     * (non-PHPdoc)
     *
     * @see \ata\api\IAddress::deleteDistrict()
     */
    public function deleteDistrict($district_id)
    {
        $district = M('sys_district');
        return $district->delete($district_id);
    }


    function express(){
        $express=M('express');
        $expressarray= $express->select();
        $this->assign('expressarray',$expressarray);
        $this->assign('title','物流公司');
        $this->display();
    }

    function expressadd(){
        if($_POST){
            $data=I('post.');
            $express=M('express');
            if($data['is_default']){
                $data['is_default']=1;
                $default=$express->where('is_default=1')->select();
                foreach ($default as $key => $value) {
                     $express->where('id='.$value['id'])->setField('is_default',0);
                }
               
            }else{
                $data['is_default']=0;
            }
            if($data['is_enabled']){
                $data['is_enabled']=1;
            }else{
                $data['is_enabled']=0;
            }
            if($data['id']){
                $rs=$express->save($data);
            }else{
                $rs=$express->add($data);
            }
            if($rs){
                if($data['id']==null){
                     echo "0|||添加成功|||/System/express.html";die;
                }else{
                     echo "0|||更新成功|||/System/express.html";die;
                    
                }
            }else{
                if($data['id']==null){
                     echo "0|||添加失败";die;
                }else{
                     echo "0|||更新失败";die;
                    
                }
            }
            
        }else{
            if($_GET['id']){
                 $express=M('express');
                 $expressarray=$express->find($_GET['id']);
            }
            $this->assign('expressarray',$expressarray);
            $this->assign('title','添加物流公司');
            $this->display(); 
        }
       
    }

    function expressdel(){
        if($_GET['id']){
                 $express=M('express');
                 $expressarray=$express->delete($_GET['id']);
        }
        $this->redirect('System/express');
    }
    
    
}